package com.zuper.demo.sessionlogin.config;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zuper.demo.sessionlogin.base.BaseResult;
import com.zuper.demo.sessionlogin.exception.BusinessException;

@ControllerAdvice(
		basePackages = {"com.**.web.*"}, 
		annotations = {Controller.class, RestController.class})
public class MyCtrllerAdvice {
	
	//全局异常处理-ajax-json
	@ExceptionHandler(value=Exception.class)
	@ResponseBody
	public BaseResult exceptionForAjax(Exception ex) {
		if(ex instanceof BusinessException) {
			return new BaseResult((BusinessException)ex);
		}else {
			return new BaseResult(ex.getCause()==null?ex.getMessage():ex.getCause().getMessage());
		}
	}
}
